<?php

namespace App\Store\Controllers\Sysset\Commission;

use App\Common\Services\Store\Common as StoreCommon;
use App\Store\Models\Basic\GoodsCategory;
use App\Store\Models\Sysset\Commission\Discount;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Store\Validate\Sysset\Commission\Discount as ValidateDiscount;

/**
 * 折扣
 */
class DiscountController extends Controller
{
    /**
     * 新增
     */
    public function dopostadd(Request $request)
    {
        $req_data = $request->only(['store_ids', 'goods_category_names', 'discount_min', 'discount_max', 'commission_rate_1', 'commission_rate_2']);
        $Validate = new ValidateDiscount('add');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $store_ids = $req_data['store_ids'];
        $store_ids = is_array($store_ids) ? $store_ids : (is_array(json_decode($store_ids, true)) ? json_decode($store_ids, true) : []);

        $goods_category_names = $req_data['goods_category_names'];
        $goods_category_names = is_array($goods_category_names) ? $goods_category_names : (is_array(json_decode($goods_category_names, true)) ? json_decode($goods_category_names, true) : []);

        // 组装数据
        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $cp_id = $accountinfo['cp_id'];

        foreach ($store_ids as $store_id) {
            $discount = [];
            foreach ($goods_category_names as $k => $goods_category_name) {
                $one = GoodsCategory::where([
                    'store_id' => $store_id,
                    'category_name' => $goods_category_name,
                     'is_del'=>1
                ])->first();
                if ($one) {
                    $one = $one->toArray();
                    $goods_category_id = $one['id'];

                    // Check if the combination of store_id and goods_category_id already exists
                    $existing_discount = Discount::where([
                        'cp_id' => $cp_id,
                        'store_id' => $store_id,
                        'goods_category_id' => $goods_category_id,
                        'is_del' => 1
                    ])->first();

                    if ($existing_discount) {
                        return response()->json(['code' => '001', 'msg' => '商品类别 ' . $goods_category_name . ' 的配置已存在']);
                    }
                    $goods_category_id = $one['id'];
                    $commission_rate_1 = !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0;
                    $commission_rate_2 = !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0;

                    $discount[$k]['cp_id']                    = $cp_id;
                    $discount[$k]['store_id']                 = $store_id;
                    $discount[$k]['goods_category_id']        = $goods_category_id;
                    $discount[$k]['discount_min']             = $req_data['discount_min'];
                    $discount[$k]['discount_max']             = $req_data['discount_max'];
                    $discount[$k]['commission_rate']          = $commission_rate_1 + $commission_rate_2;
                    $discount[$k]['commission_rate_1']        = $commission_rate_1;
                    $discount[$k]['commission_rate_2']        = $commission_rate_2;
                    $discount[$k]['create_account_id']        = $accountinfo['id'];
                    $discount[$k]['create_account_real_name'] = $accountinfo['real_name'];
                    $discount[$k]['create_at']                = time();
                }
            }
            if ($discount) Discount::insert($discount);
        }

        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 列表
     */
    public function getlist(Request $request)
    {
        $list = Discount::getList($request);
        return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $list]);
    }

    /**
     * 详情
     */
    public function getone(Request $request)
    {
        $one = Discount::where('id', $request['id'])->first();
        if ($one) {
            $one = $one->toArray();
            return response()->json(['code' => '000', 'msg' => '获取成功', 'data' => $one]);
        }
        return response()->json(['code' => '000', 'msg' => '获取失败']);
    }

    /**
     * 编辑
     */
    public function dopostedit(Request $request)
    {
        $req_data = $request->only(['store_id', 'goods_category_id', 'discount_min', 'discount_max', 'commission_rate_1', 'commission_rate_2']);
        $Validate = new ValidateDiscount('edit');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $commission_rate_1 = !empty($req_data['commission_rate_1']) ? $req_data['commission_rate_1'] : 0;
        $commission_rate_2 = !empty($req_data['commission_rate_2']) ? $req_data['commission_rate_2'] : 0;
        $req_data['commission_rate']          = $commission_rate_1 + $commission_rate_2;
        $req_data['commission_rate_1']        = $commission_rate_1;
        $req_data['commission_rate_2']        = $commission_rate_2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Discount::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }

    /**
     * 删除
     */
    public function dodel(Request $request)
    {
        $req_data = $request->only(['id']);
        $Validate = new ValidateDiscount('del');
        if (!$Validate->check($req_data)) {
            return response()->json(['code' => '001', 'msg' => $Validate->getError()]);
        }

        $accountinfo = StoreCommon::getCurrentAccountinfo($request);
        $req_data['is_del']                   = 2;
        $req_data['update_account_id']        = $accountinfo['id'];
        $req_data['update_account_real_name'] = $accountinfo['real_name'];
        $req_data['update_at']                = time();

        Discount::where('id', $request['id'])->update($req_data);
        return response()->json(['code' => '000', 'msg' => '操作成功']);
    }
}
